package com.jdz.gxq.info.common.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.upload.FormFile;

import com.jdz.gxq.info.admin.file.bo.FileInfo;

public class FileUtils {

	//private final static String FILE_MAX_SIZE = "file.max.size";

	public static FileInfo uploadFile(String realPath, FormFile fileup) throws FileNotFoundException, IOException {

		FileInfo fileInfo = new FileInfo();
		
		// 可以放到属性文件中设置大小
/*		String maxSizeFileStr = PropertyUtil.getProperty(FILE_MAX_SIZE);
*/
		if (StringUtils.isEmpty(realPath)) {
			return fileInfo;
		}
		
		pathCheck(realPath);

/*		int maxSizeFile = Integer.parseInt(maxSizeFileStr);
*/
		if (fileup == null) {
			return fileInfo;
		}
		
		String fileName = fileup.getFileName();
		
/*		int fileSize = fileup.getFileSize();*/
		
		if (StringUtils.isEmpty(fileName)) {
			fileup.destroy();
			return fileInfo;
		}
		
/*		if (fileSize < 0 || fileSize > maxSizeFile) {
			fileup.destroy();
			return fileInfo;
		}*/

		// 文件上传读写操作
		InputStream is = fileup.getInputStream();
		BufferedInputStream inBuffer = new BufferedInputStream(is);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
		Random r = new Random();
		
		String oldFileName = fileup.getFileName();
		
		String ext = oldFileName.substring(oldFileName.lastIndexOf("."), oldFileName.length());
		
		String fileid = sdf.format(new Date()) + r.nextInt(100);
		String newFileName = fileid + ext;
		FileOutputStream fos = new FileOutputStream(realPath + "/" + newFileName);

		BufferedOutputStream outBuffer = new BufferedOutputStream(fos);

		int data = 0;
		while ((data = inBuffer.read()) != -1) {
			outBuffer.write(data);
		}

		outBuffer.flush();
		inBuffer.close();
		outBuffer.close();
		fileup.destroy();
		
		fileInfo.setFileid(fileid);
		fileInfo.setFileName(newFileName);
		fileInfo.setRealPath(realPath);
		
		return fileInfo;
	}
	
	/**
	 * 删除文件
	 * @param realPath
	 * @param fileName
	 */
	public static void deleteFile(String filePath) {
		 File myDelFile = new File(filePath); 
		 
		 System.out.println(filePath);
		 
		 if (myDelFile.isFile()) {
			 System.out.println("hello");
		 }
         myDelFile.delete(); 
	}

	/**
	 * 转换文件大小格式
	 * 
	 * @param fileSize
	 * @return
	 */
/*	private static String sizeToString(int fileSize) {
		String fileSizeStr = "";
		if (fileSize / 1000000 > 0) {
			fileSizeStr = Integer.toString(fileSize / 1000000).concat("M");
		} else if (fileSize / 1000 > 0) {
			fileSizeStr = Integer.toString(fileSize / 1000).concat("K");
		} else {
			fileSizeStr = Integer.toString(fileSize);
		}
		return fileSizeStr;
	}
*/
	/**
	 * 验证目录是否存在
	 * 
	 * @param path
	 */
	private static void pathCheck(String path) {
		File pfile = null;
		pfile = new File(path);
		if (!pfile.exists()) {
			pfile.mkdirs();
		}
	}
}
